﻿Public Class DRUGAGENTCLASS
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Private AGENTCODE As String = Nothing
    Private COMPANY_NAME As String = Nothing
    Private CONTACT_NAME As String = Nothing
    Private ADDRESS As String = Nothing
    Private POSTCODE As String = Nothing
    Private TEL As String = Nothing
    Private MTEL As String = Nothing
    Private EMAIL As String = Nothing
    Private FAX As String = Nothing
    Private IDBANK As String = Nothing
    Private LIMITAMOUNT As Integer = Nothing
    Private CREDITTERM As Integer = Nothing
    Private LEADTIME As Integer = Nothing
    Private STATUS = Nothing
    Private TAX As Integer = Nothing
    Private TAXRATE As Double = Nothing
    Private TAXID As String = Nothing
    Private selectAgent As String = "SELECT `agentcode` AS 'รหัสผู้ค้า', `company_name` AS 'ชื่อผู้ค้า', `contact_name` AS 'ชื่อตัวแทน', `address` AS 'ที่อยู่', `postcode` AS 'รหัสไปรษณีย์', `tel` AS 'โทรศัพท์', `mtel` AS 'โทรศัพท์มือถือ', `email` AS 'E-mail', `fax` AS 'แฟ็กซ์', `idbank` AS 'บัญชีธนาคาร', `limitamount` AS 'วงเงิน', `creditterm` AS 'เครดิต', `leadtime` AS 'ระยะเวลาส่งของ', CASE tax WHEN '1' THEN 'Without Tax' WHEN '2' THEN 'Include' WHEN '3' THEN 'Exclude' ELSE 'Unknown' END AS TAX,  `taxrate` AS 'อัตราภาษี', `taxid` AS 'เลขที่ผู้เสียภาษี', `dinsert` AS 'วันที่เพิ่มข้อมูล', `status` AS 'สถานะการเปิดใช้งาน' FROM agent WHERE agentype <> ''"

    Public Property AGENTCODE_ As String
        Get
            Return AGENTCODE
        End Get
        Set(value As String)
            AGENTCODE = value
        End Set
    End Property

    Public Property COMPANY_NAME_ As String
        Get
            Return COMPANY_NAME
        End Get
        Set(value As String)
            COMPANY_NAME = value
        End Set
    End Property

    Public Property CONTACT_NAME_ As String
        Get
            Return CONTACT_NAME
        End Get
        Set(value As String)
            CONTACT_NAME = value
        End Set
    End Property

    Public Property ADDRESS_ As String
        Get
            Return ADDRESS
        End Get
        Set(value As String)
            ADDRESS = value
        End Set
    End Property

    Public Property POSTCODE_ As String
        Get
            Return POSTCODE
        End Get
        Set(value As String)
            POSTCODE = value
        End Set
    End Property

    Public Property TEL_ As String
        Get
            Return TEL
        End Get
        Set(value As String)
            TEL = value
        End Set
    End Property

    Public Property MTEL_ As String
        Get
            Return MTEL
        End Get
        Set(value As String)
            MTEL = value
        End Set
    End Property

    Public Property EMAIL_ As String
        Get
            Return EMAIL
        End Get
        Set(value As String)
            EMAIL = value
        End Set
    End Property

    Public Property FAX_ As String
        Get
            Return FAX
        End Get
        Set(value As String)
            FAX = value
        End Set
    End Property

    Public Property IDBANK_ As String
        Get
            Return IDBANK
        End Get
        Set(value As String)
            IDBANK = value
        End Set
    End Property

    Public Property LIMITAMOUNT_ As Integer
        Get
            Return LIMITAMOUNT
        End Get
        Set(value As Integer)
            LIMITAMOUNT = value
        End Set
    End Property

    Public Property CREDITTERM_ As Integer
        Get
            Return CREDITTERM
        End Get
        Set(value As Integer)
            CREDITTERM = value
        End Set
    End Property

    Public Property LEADTIME_ As Integer
        Get
            Return LEADTIME
        End Get
        Set(value As Integer)
            LEADTIME = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Public Property TAX_ As Integer
        Get
            Return TAX
        End Get
        Set(value As Integer)
            TAX = value
        End Set
    End Property

    Public Property TAXRATE_ As Double
        Get
            Return TAXRATE
        End Get
        Set(value As Double)
            TAXRATE = value
        End Set
    End Property

    Public Property TAXID_ As String
        Get
            Return TAXID
        End Get
        Set(value As String)
            TAXID = value
        End Set
    End Property

    Public Property selectAgent_ As String
        Get
            Return selectAgent
        End Get
        Set(value As String)

        End Set
    End Property

    Public Function insertAgent() As Boolean
        If connect.GetTable("SELECT agentcode FROM agent WHERE company_name = '" & COMPANY_NAME & "'").Rows.Count > 0 Then
            MsgBox("ไม่สามารถเพิ่มผู้ค้ารายนี้ได้อีก เนื่องจากมีข้อมูลผู้ค้านี้ในระบบอยู่แล้ว")
        Else
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()

                connect.ExecuteNonQuery("INSERT INTO agent (agentcode, company_name, contact_name, address, postcode, tel, mtel, email, fax, idbank, limitamount, creditterm, leadtime, status, tax, taxrate, taxid, dinsert) VALUES ('" & AGENTCODE & "', '" & COMPANY_NAME & "', '" & CONTACT_NAME & "', '" & ADDRESS & "', '" & POSTCODE & "', '" & TEL & "', '" & MTEL & "', '" & EMAIL & "', '" & FAX & "', '" & IDBANK & "', '" & LIMITAMOUNT & "', '" & CREDITTERM & "', '" & LEADTIME & "', '" & STATUS & "', '" & TAX & "', '" & TAXRATE & "', '" & TAXID & "', NOW());")
                'connectlocal.ExecuteNonQuery("INSERT INTO MASCOUNTRY ([CODECOUNTRY], [COUNTRY]) VALUES (" & countryCODECOUNTRY & ", '" & countryCOUNTRY & "');")
                MsgBox("ท่านได้ทำการเพิ่มข้อมูลผู้ค้าเรียบร้อยแล้ว")

                Return True
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
                Return False
            End Try
            'connectlocal.Dispose()
            connect.Dispose()
        End If
    End Function

    Public Function editAgent() As Boolean
        If connect.GetTable("SELECT agentcode FROM agent WHERE agentcode = '" & AGENTCODE & "' AND company_name = '" & COMPANY_NAME & "' AND contact_name = '" & CONTACT_NAME & "' AND address = '" & ADDRESS & "' AND postcode = '" & POSTCODE & "' AND tel = '" & TEL & "' AND mtel = '" & MTEL & "' AND email = '" & EMAIL & "' AND fax = '" & FAX & "' AND idbank = '" & IDBANK & "' AND limitamount = '" & LIMITAMOUNT & "' AND creditterm = '" & CREDITTERM & "' AND leadtime = '" & LEADTIME & "' AND status = '" & STATUS & "' AND tax = '" & TAX & "' AND taxrate = '" & TAXRATE & "' AND taxid = '" & TAXID & "'").Rows.Count > 0 Then
            MsgBox("ไม่สามารถแก้ไขข้อมูลผู้ค้ายาและเวชภัณฑ์นี้ได้ เนื่องจากไม่ได้มีการเปลี่ยนแปลงข้อมูล")
        Else
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()

                connect.ExecuteScalar("UPDATE agent SET company_name = '" & COMPANY_NAME & "', contact_name = '" & CONTACT_NAME & "', address = '" & ADDRESS & "', postcode = '" & POSTCODE & "', tel = '" & TEL & "', mtel = '" & MTEL & "', email = '" & EMAIL & "', fax = '" & FAX & "', idbank = '" & IDBANK & "', limitamount = '" & LIMITAMOUNT & "', creditterm = '" & CREDITTERM & "', leadtime = '" & LEADTIME & "', status = '" & STATUS & "', tax = '" & TAX & "', taxrate = '" & TAXRATE & "', taxid = '" & TAXID & "' WHERE agentcode = '" & AGENTCODE & "'")
                'connectlocal.ExecuteNonQuery("INSERT INTO MASCOUNTRY ([CODECOUNTRY], [COUNTRY]) VALUES (" & countryCODECOUNTRY & ", '" & countryCOUNTRY & "');")
                MsgBox("ท่านได้ทำการแก้ไขข้อมูลผู้ค้ายาและเวชภัณฑ์เรียบร้อยแล้ว")

                Return True
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
                Return False
            End Try
            'connectlocal.Dispose()
            connect.Dispose()
        End If
    End Function
End Class
